package ru.ifmo.tanya.service;

import ru.ifmo.tanya.core.users.User;
import ru.ifmo.tanya.core.users.UserKeeper;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


public abstract class AuthorizedHttpService implements HttpService {

    private UserKeeper userKeeper = new UserKeeper();

    @Override
    public void invoke(HttpServletRequest request, HttpServletResponse response) throws IOException {
        User user = userKeeper.extractUser(request);
        if (user == null) {
            try {
                response.sendRedirect("/");
                return;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        invokeWithUser(user, request, response);
    }

    public abstract void invokeWithUser(User user, HttpServletRequest request, HttpServletResponse response) throws IOException;
}
